package com.efrei.uchat.message;

import com.efrei.uchat.exception.UChatException;

@SuppressWarnings("rawtypes")
public enum Headers {
	/**
	 * Message with no type
	 */
	UNKNOWN("UNKNOWN", Message.class),
	
	// Text messages to public/specific croom
	
	TEXT("TEXT", TextMessage.class),
	TEXT_CHANNEL("TEXT_C", TextMessage.class),

	// SERVICE Messages
	
	LOGIN("LOGIN", LoginMessage.class),
	CROOM_MESSAGE("CROOM", CroomMessage.class),
	CROOM_LIST_MESSAGE("CROOM_LS", CroomsMessage.class),
	CROOM_USERS_MESSAGE("CROOM_USERS", CroomUsersMessage.class);

	private static int NB_MESSAGE_TYPE = 0;
	private int code;
	private String text;
	private Class classManager;
	
	static {
		NB_MESSAGE_TYPE = 0;
	}
	
	Headers(String text, Class classManager) {
		this.code = getNewMessageType();
		this.text = text;
		this.classManager = classManager;
	}
	
	private static int getNewMessageType() {
		return NB_MESSAGE_TYPE++;
	}
	
	public int getCode() {
		return code;
	}
	
	public String getText() {
		return text;
	}
	
	public Class getClassManager() {
		return classManager;
	}
	
	@Override
	public String toString() {
		return text;
	}

	public static Headers getFromText(String text) throws UChatException {
		for (Headers message : Headers.values()) {
			if(message.toString().equals(text)) {
				return message;
			}
		}
		
		throw new UChatException(text+" MessageType unknown");
	}
}
